//
//  CSQrcodeWebViewController.m
//  JiFengDaRen
//
//  Created by SH_iOS on 2017/10/18.
//  Copyright © 2017年 SH_iOS. All rights reserved.
//

#import "CSQrcodeWebViewController.h"

@interface CSQrcodeWebViewController ()<UIWebViewDelegate>
{
    UIWebView *_webView;
}
@end

@implementation CSQrcodeWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
    // Do any additional setup after loading the view.
}


#pragma mark -- 初始化UI
- (void)configUI{
    
    self.view.backgroundColor = [UIColor whiteColor];
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0,0,KScreenWidth, KScreenHeight -  KNAVIGATIONBARHEIGHT )];
    //设置代理
    _webView.delegate = self;
    _webView.opaque = YES;
    
    //缩放整个scrollview
    _webView.scalesPageToFit = YES;
    
    
    //加载网页
    
    
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlString]]];
    
    
    
    [self.view addSubview:_webView];
    
    UIBarButtonItem *cutScreen = [[UIBarButtonItem alloc]initWithTitle:@"截屏" style:UIBarButtonItemStylePlain target:self action:@selector(cutScreenQrcode:)];
    [cutScreen setWidth:-5];
    [cutScreen setTitleTextAttributes:@{NSFontAttributeName:KSHSystemFont(15),NSForegroundColorAttributeName:KREDCOLOR} forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = cutScreen;
    
    
    
    
}

- (void)cutScreenQrcode:(UIBarButtonItem *)item{
    
    
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
    //    UIGraphicsBeginImageContext(self.view.bounds.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image= UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    
    UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
    
    [self.view makeToast:@"截图成功,已保存到本地相册" duration:2.f position:@"centter"];
    
    
}
#pragma mark -- 返回
- (void)back:(UIBarButtonItem *)item{
    
    [self goToCack];
}

#pragma mark -- 返回
- (void)goToCack{
    
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    
    
    if (_webView.canGoBack) {
        
        [_webView goBack];
        
    }else{
        [self.navigationController popViewControllerAnimated:YES];
        
    }
    
}
#pragma mark - WebViewDelegate
//加载失败
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    SHLog(@"加载失败%@",error.localizedDescription);
}

//网页加载完成
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSLog(@"加载成功！");

    //运行js代码
    [_webView stringByEvaluatingJavaScriptFromString:
     @"var script = document.createElement('script');"
     "script.type = 'text/javascript';"
     "script.text = \"function ResizeImages(screenWidth) { "
     "var myimg,oldwidth;"
     "var maxwidth= 375;" //缩放系数
     "for(i=0;i <document.images.length;i++){"
     "myimg = document.images[i];"
     "if(myimg.width > screenWidth){"
     "oldwidth = myimg.width;"
     "myimg.width = screenWidth;"
     "myimg.height = myimg.height * (screenWidth/oldwidth);"
     "}"
     "}"
     "}\";"
     "document.getElementsByTagName('head')[0].appendChild(script);"
     ];


   [_webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"ResizeImages(%f)",self.view.frame.size.width-100]];


}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
